package cn.sx.service;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc: 商品服务
 *@Time: 2020/9/20  12:13
 */

import cn.sx.dao.GoodsDao;
import cn.sx.entity.Goods;
import cn.sx.entity.Tops;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private TypesService typesService;
    @Autowired
    private TopService topService;


    /*后台管理显示查询产品*/
    public List<Goods>selectAllGoods(int page,int size){
        return packGood(goodsDao.selectAllGoods(size * (page-1), size));
    }

    /*根据id查询商品信息*/
    public Goods get(int id){
        return packGood(goodsDao.select(id));
    }

    /* 商品分类*/
    public List<Goods> getListByType(int typeId, int page, int size) {
        return typeId > 0 ? packGood(goodsDao.selectListByTypeId(typeId,
                size * (page - 1),size)) : this.getList(page,size);
    }

    /*获取数量（今日推荐）*/
    public long getCountByTopType(byte type){
        return goodsDao.selectCountByTopType(type);
    }

    /*新品上市*/
    public List<Goods> getList(int page, int size) {
        return packGood(goodsDao.selectList(size * (page-1),size));
    }

    /*封装商品*/
    private List<Goods> packGood(List<Goods> list) {
        for (Goods good:list) {
            good=packGood(good);
        }
        return list;
    }

    /*封装商品 */
    private Goods packGood(Goods goods){
        if (goods !=null){
            goods.setType(typesService.get(goods.getTypeId()));//类目信息
            goods.setTop(Objects.nonNull(topService.getByGoodIdAndType(goods.getId(), Tops.TYPE_TODAY)));
        }
        return goods;
    }

    /*获取数量（商品分类（显示商品） ）*/
    public long getCountByType(int typeId) {
        return typeId > 0 ? goodsDao.selectCountByTypeId(typeId):this.getCount();
    }

    /*获取总数*/
    public  int getCount() {
        return goodsDao.selectCount();
    }

    public boolean update(Goods goods){
        return goodsDao.update(goods);
    }
    /*更新订单*/
    public boolean updateStock(int id ,int stock){
        return goodsDao.updateStock(id,stock);
    }

    /*修改销量*/
    public boolean updateSales(int id,int sales){
        return goodsDao.updateSales(id,sales);
    }

    /*删除商品*/
    @Transactional//开启事务
    public boolean delete(int id){
        topService.deleteByGoodId(id);//先删除此商品的推荐信息
        return goodsDao.delete(id);
    }
    /*通过推荐搜索（今日推荐）*/
    public List<Goods> getListByTopType(byte type, int page, int size) {

        return packGood(goodsDao.selectListByTopType(type,size*(page-1),size));
    }
    /*热销排行*/
    public List<Goods> getListOrderSales(int page, int size) {
        return packGood(goodsDao.getListOrderSales(size*(page-1),size));
    }

    public List<Goods> getListByIds(String ids, int page, int size) {
        return Objects.nonNull(ids) ? packGood(goodsDao.selectListByIds
                (ids,size*(page-1),size)) : null;
    }

    public int getCountByIds(String goodIds) {
        return goodsDao.selectCountByIds(goodIds);
    }

    public boolean add(Goods goods) {
        return goodsDao.insert(goods);
    }


    /*修改后台商品信息*/

}
